/*
 * CustomBuildingListViewAdapter allows an Android ListView object to display Building data 
 * in a clean, uniform format
 */
package com.example.wmuqrreader;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
 
public class CustomBuildingListViewAdapter extends ArrayAdapter<Building> {
 
    Context context;
 
    public CustomBuildingListViewAdapter(Context context, int resourceId, List<Building> items) {
        super(context, resourceId, items);
        this.context = context;
    }
 
    /*private view holder class*/
    private class ViewHolder {
    	TextView txtNum;
    	TextView txtLocation;
        TextView txtId;
    }
 
    public View getView(int position, View convertView, ViewGroup parent) {
    	ViewHolder holder = null;
        Building rowItem = getItem(position);
 
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.simple_list_building, null);
            holder = new ViewHolder();
            
            holder.txtNum		= (TextView) convertView.findViewById(R.id.textView_num);
            holder.txtLocation 	= (TextView) convertView.findViewById(R.id.textView_building);
            holder.txtId		= (TextView) convertView.findViewById(R.id.textView_id);
            
            convertView.setTag(holder);
        }
        else
            holder = (ViewHolder) convertView.getTag();
 
        
        //TODO: the values of NUM & ID seem to be transposed. They have been temporarily switched
        holder.txtNum.setText( String.format("%4s:", Integer.toString(rowItem.get_id()) ).replace(' ', '0') );
        holder.txtLocation.setText(rowItem.get_building());
        holder.txtId.setText( Integer.toString(rowItem.get_num()) );
 
        return convertView;
    }
}
